package com.yuwenchao.superafflatus.controller;

import cn.hutool.core.util.IdUtil;
import com.yuwenchao.superafflatus.seal.Seal;
import com.yuwenchao.superafflatus.seal.SealFont;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

@Slf4j
@Api(tags = "印章功能测试")
@Controller
public class SealController {

   // String path ="src/main/resources/static/SealImg/";

    //从配置文件中取路径值   这里动态获取
    @Value("${linuxFile.downloadFile}")
    private String downloadFile;


    /**
     *这里生成图片后不能立即下载,因为生生图片后在项目中,必须下次启动后才能获取到下载的文件.
     */


/*@ApiOperation("获取印章图片")
@GetMapping("/getSealPng")
//@ResponseBody
public void getSealFile(String name, HttpServletResponse response) throws Exception {

    File dir = new File(downloadFile);
    if (!dir.exists()) {
        dir.mkdirs();
    }

   //生成图片并命名
    String simpleUUID = IdUtil.simpleUUID().substring(0,5);
    String sealFileNme= downloadFile + name + ".png";
    System.out.println("获取到的路径"+sealFileNme);
    Seal.builder()
            .size(300)
            .borderSquare(16)
            .mainFont(SealFont.builder().text(name).size(120).build())
            .build()
            .draw(sealFileNme);
}*/


    @ApiOperation(value = "印章图片生成及下载")
    @GetMapping("/downSealFile")
    public void downSealFile(String name, HttpServletResponse response){
        File dir = new File(downloadFile);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        //生成图片并命名
        try {
            String simpleUUID = IdUtil.simpleUUID().substring(0,5);
            String sealFileNme= downloadFile + name + ".png";
            System.out.println("获取到的路径"+sealFileNme);
            Seal.builder()
                    .size(300)
                    .borderSquare(16)
                    .mainFont(SealFont.builder().text(name).size(120).build())
                    .build()
                    .draw(sealFileNme);
        } catch (Exception e) {
            System.out.println("生成异常!");
            e.printStackTrace();
        }
        //下载
        try {
            String fileName =name +".png";
            response.setHeader( "Content-Disposition", "attachment;filename="  + new String( fileName.getBytes("gb2312"), "ISO8859-1" ) );
            System.out.println("获取到的文件名"+fileName);
            if(!fileName.isEmpty()) {
                //response.addHeader("Content-Disposition", "attachment;filename=" + TxtName);
                response.setContentType("multipart/form-data");
                FileInputStream inputStream = new FileInputStream(downloadFile+fileName);
                ServletOutputStream stream = response.getOutputStream();
                IOUtils.copy(inputStream, stream);
                inputStream.close();
            }
        } catch (IOException e) {
            System.out.println("系统文件不存在!");
            e.printStackTrace();
        }
    }

}
